/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import hibernate.entity.Account;
import hibernate.entity.Collection;
import hibernate.entity.TrackedProduct;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author NamDH
 */
public class CollectionDAO {

    private Session session;

    public CollectionDAO() {
        SessionFactory sf = Common.HibernateUtil.getSessionFactory();
        session = sf.getCurrentSession();
    }

    public List<Collection> getCollectionsbyAccount_Id(int account_Id) {
        try {

            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();

            session.getTransaction().begin();
            String sql = "from Collection where AccountID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, account_Id);
            List<Collection> result = query.list();
            session.getTransaction().commit();
            for (int i = 0; i < result.size(); i++) {
                Collection co = result.get(i);
                session.delete(co);
            }
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public boolean createCollection(int accountID, String collectionName, String description) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account account = (Account) session.get(Account.class, accountID);
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            String createdDate = dateFormat.format(date);
            Collection col = new Collection(account, collectionName, description, createdDate);
            session.save(col);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean removeItem(int trackedProductID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            TrackedProduct tp = (TrackedProduct) session.get(TrackedProduct.class, trackedProductID);
            Collection col = new Collection();
            col.setCollectionID(14);
            tp.setCollection(col);
            session.update(tp);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public boolean addItem(int collectionID, int trackedProductID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            TrackedProduct tp = (TrackedProduct) session.get(TrackedProduct.class, trackedProductID);
            Collection col = new Collection();
            col.setCollectionID(collectionID);
            tp.setCollection(col);
            session.update(tp);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteCollection(int collectionID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Collection col = (Collection) session.get(Collection.class, collectionID);
            session.delete(col);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public Collection getCollectionByName(String collection_Name) {
        Collection collection = new Collection();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Collection Where CollectionName = ?";
            Query query = session.createQuery(sql);
            query.setString(0, collection_Name);
            collection = (Collection) query.uniqueResult();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        }
        return collection;
    }

    public Collection getCollectionByID(int collection_ID) {
        Collection collection = new Collection();
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Collection Where CollectionID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, collection_ID);
            collection = (Collection) query.uniqueResult();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return null;
        }
        return collection;
    }

    public boolean updateCollection(String collectionName, String description, int collectionID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Collection col = (Collection) session.get(Collection.class, collectionID);
            col.setCollectionName(collectionName);
            col.setDescription(description);
            session.update(col);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

     public boolean deleteCollectionbyAccountID(int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "delete Collection where AccountID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, accountID);
            query.executeUpdate();
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }
}
